Imports System.ComponentModel
Imports DevExpress.XtraBars
Imports Phoenix.Preferences

Namespace Forms

    Public Class PhoenixFormBase
        Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            '
            'PhoenixFormBase
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(784, 430)
            Me.Name = "PhoenixFormBase"
            Me.Text = "PhoenixFormBase"

        End Sub

#End Region

#Region " Preferenses "
        Protected Overridable Sub LoadPreferences(ByVal pref As IPreferencesNode)
            Me.Width = pref.GetInt32("Width", Me.Width)
            Me.Height = pref.GetInt32("Height", Me.Height)
            Me.Top = pref.GetInt32("Top", Me.Top)
            Me.Left = pref.GetInt32("Left", Me.Left)
        End Sub

        Protected Overridable Sub SavePreferences(ByVal pref As IPreferencesNode)
            If Me.WindowState <> FormWindowState.Minimized Then
                pref("Width") = Me.Width
                pref("Height") = Me.Height
                pref("Top") = Me.Top
                pref("Left") = Me.Left
            End If
        End Sub

        Protected Overrides Sub OnLoad(ByVal e As EventArgs)
            MyBase.OnLoad(e)

            If Not MyBase.DesignMode Then
                Try
                    DoLoadPreferences()
                Catch ex As Exception
                    Trace.WriteLine(ex.ToString)
                End Try
            End If
        End Sub

        Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)
            If Not MyBase.DesignMode Then
                Try
                    DoSavePreferences()
                Catch ex As Exception
                    Trace.WriteLine(ex.ToString)
                End Try
            End If

            MyBase.OnClosing(e)
        End Sub

        Protected Sub DoSavePreferences()
            Dim node As IPreferencesNode
            Try
                node = AppPreferences.GetNodeForPath(Me.Name)
                SavePreferences(node)
            Catch ex As Exception
                Trace.WriteIf(AppError, ex)
                If Not node Is Nothing Then node.RemoveAllPreferences()
            End Try
        End Sub

        Protected Sub DoLoadPreferences()
            SuspendLayout()

            Dim node As IPreferencesNode
            Try
                node = AppPreferences.GetNodeForPath(Me.Name)
                LoadPreferences(node)
            Catch ex As Exception
                Trace.WriteIf(AppError, ex)
                If Not node Is Nothing Then node.RemoveAllPreferences()
            End Try
            ResumeLayout()

        End Sub

#End Region

#Region " Common Stuff "
        Protected Sub BindWithCapitalization(ByVal targetBind As Control, ByVal targetProperty As String, ByVal objBind As Object, ByVal objProperty As String)
            Dim nameBinding As New Binding(targetProperty, objBind, objProperty)
            AddHandler nameBinding.Parse, AddressOf ConvertToCapitalizeWords
            targetBind.DataBindings.Add(nameBinding)
        End Sub

        Private Sub ConvertToCapitalizeWords(ByVal sender As Object, ByVal args As ConvertEventArgs)
            Dim name As String = args.Value

            Dim words As String() = name.Split(" ")

            For i As Long = 0 To words.Length - 1
                If words(i).Length > 0 Then
                    Dim last As String = words(i).Substring(1).ToLower()
                    words(i) = words(i).Substring(0, 1).ToUpper() & last
                End If
            Next

            args.Value = String.Join(" ", words)
        End Sub
#End Region

    End Class

End Namespace